home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / folder watching / fw receiver / windows.c < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.6 KB  |  405 lines

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright Â© 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 26/01/96    GS                FW Receiver
  21.                                             Changed size of window.
  22.                                             Changed column widths in list.
  23.                                             Added gWindow global - holds window.
  24.                                             Removed example list contents.
  25.                                             Added AddToListContent() routine to add items to
  26.                                             list.
  27.     
  28.                 12/18/95    CW                First release
  29.                 
  30.  
  31. */
  32.  
  33.  
  34. #pragma segment Core
  35. #include <Sound.h>
  36.  
  37.  
  38. // System Includes
  39.  
  40. #ifndef __TYPES__
  41.     #include <Types.h>
  42. #endif
  43.  
  44. #ifndef __WINDOWS__
  45.     #include <Windows.h>
  46. #endif
  47.  
  48. #ifndef __QUICKDRAW__
  49.     #include <Quickdraw.h>
  50. #endif
  51.  
  52. #ifndef __RESOURCES__
  53.     #include <Resources.h>
  54. #endif
  55.  
  56. #ifndef __FONTS__
  57.     #include <Fonts.h>
  58. #endif
  59.  
  60. #ifndef __ERRORS__
  61.     #include <Errors.h>
  62. #endif
  63.  
  64.  
  65.  
  66.  
  67. // Application Includes
  68.  
  69. #ifndef __BAREBONES__
  70.     #include "BareBones.h"
  71. #endif
  72.  
  73. #ifndef __PROTOTYPES__
  74.     #include "Prototypes.h"
  75. #endif
  76.  
  77.  
  78. // Globals
  79.  
  80. WindowRef    gWindow = NULL;
  81.  
  82.  
  83. // Static prototypes
  84.  
  85. static OSErr    CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon );
  86. static OSErr    AddListContents ( ListRef theList, void* refCon );
  87.  
  88.  
  89. OSErr            PatchListLDEF ( ListRef theList );
  90.  
  91.  
  92.  
  93.  
  94.  
  95. //
  96. // This is called to create the application's window.
  97. //
  98. void CreateWindow ( void )
  99. {
  100.     OSErr        theErr;
  101.     WindowRef    theWindow;
  102.     
  103.     theErr = CreateListWindow ( &theWindow, AddListContents, nil );
  104.     if ( theErr )
  105.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  106.  
  107.     gWindow = theWindow;
  108.     
  109.     return;
  110. }
  111.  
  112.  
  113.  
  114. //
  115. // This will close the application's window, dispose of any storage we've hung
  116. // off the window, and then dispose of the window itself.
  117. //
  118. WindowRef DestroyWindow ( WindowRef windowRef )
  119. {
  120.     if ( windowRef )
  121.     {
  122.         ListRef theList;
  123.         
  124.         theList = (ListRef) GetWRefCon ( windowRef );
  125.         if ( theList )
  126.             LDispose ( theList );
  127.         
  128.         DisposeWindow ( windowRef );
  129.         
  130.     }
  131.     
  132.     return nil;
  133. }
  134.  
  135.  
  136.  
  137. //
  138. // Creates a window with a list in the centre. contentsProc is a routine
  139. // pointer to intitialize the list. It's passed the refCon to assist.
  140. //
  141. OSErr CreateListWindow ( WindowRef* windowRef, tContentsProcPtr contentsProc, void* refCon )
  142. {
  143.     OSErr        theErr;
  144.     WindowRef    theWindow;
  145.     
  146.     
  147.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1 );
  148.     if ( theWindow == nil )
  149.         return (ResError ( )) ? ResError ( ) : resNotFound;
  150.     
  151.     SetPortWindowPort ( theWindow );
  152.     TextFont ( kFontIDGeneva );
  153.     TextFace ( normal );
  154.     TextSize ( 9 );
  155.     
  156.      theErr = CreateContentList ( theWindow, contentsProc, refCon );
  157.     if ( theErr )    goto CleanupAndBail;
  158.     
  159.     SelectWindow ( theWindow );
  160.     ShowWindow ( theWindow );
  161.     
  162.     *windowRef = theWindow;
  163.     
  164.     return noErr;
  165.     
  166.  
  167. CleanupAndBail:
  168.     
  169.     // Don't forget to free any storage we've used so far
  170.     DestroyWindow ( theWindow );
  171.     
  172.     return theErr;
  173. }
  174.  
  175.  
  176.  
  177. //
  178. // This is passed into the CreateDocumentWindow routine
  179. // to add the contents to the newly created list.
  180. //
  181. static OSErr AddListContents ( ListRef theList, void* refCon )
  182. {
  183. #ifdef __MWERKS__
  184.     #pragma unused( theList, refCon )
  185. #endif
  186. /*
  187.     AddToList ( theList, "\pApple Extras\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  188.     AddToList ( theList, "\pApplications\t-\tfolder\tWed, Dec 18, 1995, 12:04 pm" );
  189.     AddToList ( theList, "\pClean System Folder\t-\tfolder\tThu, Dec 7, 1995, 1:46 pm" );
  190.     AddToList ( theList, "\pCommunications\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  191.     AddToList ( theList, "\pCopland Stuff\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  192.     AddToList ( theList, "\pDevelopment\t-\tfolder\tWed, Dec 13, 1995, 4:27 pm" );
  193.     AddToList ( theList, "\pDocumentation\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  194.     AddToList ( theList, "\pDTS\t-\tfolder\tWed, Dec 18, 1995, 1:07 pm" );
  195.     AddToList ( theList, "\pMisc\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  196.     AddToList ( theList, "\pProjects\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  197.     AddToList ( theList, "\pStress\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  198.     AddToList ( theList, "\pSystem Folder\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  199.     AddToList ( theList, "\pSystem Installers\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  200.     AddToList ( theList, "\pSystem Picker\t-\tapplication program\tMon, Dec 16, 1995, 8:56 pm" );
  201.     AddToList ( theList, "\pTools\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  202.     AddToList ( theList, "\pAppleScriptâ„¢ Utilities\t-\tfolder\tThu, Dec 7, 1995, 12:46 pm" );
  203.     AddToList ( theList, "\pDebuggers\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  204.     AddToList ( theList, "\pDebugging Modern Memory Manager\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  205.     AddToList ( theList, "\pDebugging OD Part Editors\t33K\tSimpleText document\tThu, Dec 19, 1995, 12:07 pm" );
  206.     AddToList ( theList, "\pDocumentation Viewers\t-\tfolder\tMon, Dec 16, 1995, 12:56 pm" );
  207.     AddToList ( theList, "\pPartMaker 4.3\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  208.     AddToList ( theList, "\pReference\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  209.     AddToList ( theList, "\pResource Editors\t5,049K\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  210.     AddToList ( theList, "\pSDK\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  211.     AddToList ( theList, "\pSoundMgr3.2Dev.sea\t198K\tapplication program\tThu, Dec 19, 1995, 12:07 pm" );
  212. */    
  213.     
  214.     return noErr;
  215.     
  216. } // AddContents
  217.  
  218.  
  219. // Add the string and scroll down
  220.  
  221. void AddToListContent ( StringPtr pStr )
  222. {
  223.     ListRef        aList;
  224.  
  225.     if ( ! gWindow )
  226.         return;
  227.         
  228.     aList = (ListRef) GetWRefCon ( gWindow );
  229.  
  230.     AddToList ( aList, pStr );
  231.     LScroll ( 0, 1000, aList );        // Scroll down to item
  232.  
  233. } // AddToListContent
  234.  
  235. //
  236. // This is called to create the List Manager list, and initialize it.
  237. //
  238. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon )
  239. {
  240.     OSErr        theErr;
  241.     ListHandle    theList;
  242.     Point        cellSize = {0, 0};
  243.     Cell        firstCell = {0, 0};
  244.     Rect         dataRect = {0, 0, 0, 1};
  245.     Rect        viewRect;
  246.     
  247.     
  248.     // The width of a cell needs to be 32767 so any hiliting
  249.     // doesn't stop short of the window width after it's been resized.
  250.     
  251.     viewRect = theWindow->portRect;
  252.     InsetRect ( &viewRect, 20, 20 );
  253.     viewRect.right -= 15;                // minus scrollbar width
  254.     theList = LNew ( &viewRect, &dataRect, cellSize, kLDEFID, theWindow, 
  255.                         false, false, false, true );
  256.  
  257.     #ifndef USE_LDEF
  258.         theErr = PatchListLDEF ( theList );
  259.         if ( theErr )
  260.             return theErr;
  261.     #endif
  262.     
  263.     if ( theList )
  264.     {
  265.         static short    theOffsets[] = { 4, 0, 165, 230, 285 };
  266.         
  267.         (*theList)->refCon = (long) &theOffsets;
  268.         (*theList)->selFlags = lOnlyOne;
  269.         SetWRefCon ( theWindow, (long) theList );
  270.         
  271.         // Since the calling routine is always in the same architecture type as
  272.         // the callback routine, we don't need to worry about any Mixed Mode
  273.         // complications. We just treat it as a straight forward routine pointer.
  274.         if ( contentsProc )
  275.         {
  276.             theErr = (*contentsProc) ( theList, refCon );
  277.             if ( theErr )
  278.                 return theErr;
  279.         }
  280.         
  281.         LSetSelect ( true, firstCell, theList );
  282.         
  283.         // Now the list has been fully prepared, turn the drawing mode on
  284.         LSetDrawingMode ( true, theList );
  285.     }
  286.     
  287.     return noErr;
  288.     
  289. } // CreateContentList
  290.  
  291.  
  292.  
  293. void DoActivate ( EventRecord* theEvent )
  294. {
  295.     Boolean        bActiveFlag = theEvent->modifiers & resumeFlag;
  296.     GrafPtr        savePort;
  297.     WindowRef    theWindow = (WindowRef) theEvent->message;
  298.     
  299.     
  300.     GetPort ( &savePort );
  301.     SetPortWindowPort ( theWindow );
  302.     LActivate ( bActiveFlag, (ListRef) GetWRefCon ( theWindow ) );
  303.     SetPort ( savePort );
  304.     
  305.     return;
  306. }
  307.  
  308.  
  309.  
  310. void DoUpdate ( EventRecord* theEvent )
  311. {
  312.     GrafPtr            savePort;
  313.     CGrafPtr        thePort;
  314.     WindowRef        theWindow = (WindowRef) theEvent->message;
  315.     ListRef            theList = nil;
  316.     Rect            theRect;
  317.     
  318.     
  319.     
  320.     thePort = GetWindowPort ( theWindow );
  321.     
  322.     GetPort ( &savePort );
  323.     SetPortWindowPort ( theWindow );
  324.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  325.     EraseRect ( &thePort->portRect );
  326.     
  327.     theList = (ListRef) GetWRefCon ( theWindow );
  328.     if ( theList )
  329.     {
  330.         PenNormal ( );
  331.         GetListRect ( theList, &theRect );
  332.         InsetRect ( &theRect, -1, -1 );
  333.         FrameRect ( &theRect );
  334.         
  335.         LUpdate ( thePort->visRgn, theList );
  336.     }
  337.     
  338.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  339.     SetPort ( savePort );
  340.     
  341.     return;
  342. }
  343.  
  344.  
  345.  
  346. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  347. {
  348.     WindowRef    frontWindow;
  349.     
  350.     // If a movable modal is active, ignore click in an inactive 
  351.     // window, otherwise select it or handle the content click.
  352.     
  353.     frontWindow = FrontWindow ( );
  354.     if ( theWindow != frontWindow )
  355.     {
  356.         if ( IsMovableModal ( frontWindow ) )
  357.             SysBeep ( 30 );
  358.         else
  359.             SelectWindow ( theWindow );
  360.     }
  361.     else
  362.     {
  363.         GrafPtr        savePort;
  364.         
  365.         GetPort ( &savePort );
  366.         SetPortWindowPort ( theWindow );
  367.         HandleListClick ( theWindow, theEvent );
  368.         SetPort ( savePort );
  369.     }
  370.     
  371.     return;
  372.     
  373. } // DoContentClick
  374.  
  375.  
  376.  
  377. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  378. {
  379.     WindowRef    frontWindow;
  380.     
  381.     
  382.     // If a movable modal is active, ignore click in an inactive 
  383.     // title bar, otherwise let the Window Manager handle it.
  384.     
  385.     frontWindow = FrontWindow ( );
  386.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  387.         SysBeep ( 30 );
  388.     else                                
  389.     {
  390.         RgnHandle    theRgn;
  391.         Rect        dragRect;
  392.         
  393.         theRgn = GetGrayRgn ( );
  394.         dragRect = (*theRgn)->rgnBBox;
  395.         DragWindow ( theWindow, theEvent->where, &dragRect );
  396.     }
  397.     
  398.     return;
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405.